package com.neusoft.nepc.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepc.common.AlertUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


public class UserLoginViewController {
    @FXML
    private Button btn_login;
    @FXML
    private TextField txt_username;
    @FXML
    private TextField txt_password;
    @FXML
    private ImageView bgImageView; // 和 FXML 里的 fx:id 对应
    @FXML
    private AnchorPane rootAnchorPane; // 登录界面根容器，用于绑定图片尺寸
    public static String currentLoggedInUserId;

    @FXML
    public void initialize() throws FileNotFoundException {
        // 加载背景图片，路径根据实际项目结构调整
        // 假设图片放在 resources/image 目录下，Maven/Gradle 项目里 resources 会被识别为类路径根
        Image bgImage = new Image("com/neusoft/nepc/Image/envi.jpg");
        bgImageView.setImage(bgImage);

        // 让图片宽高绑定到根容器，实现自适应
//        bgImageView.fitWidthProperty().bind(rootAnchorPane.widthProperty());
//        bgImageView.fitHeightProperty().bind(rootAnchorPane.heightProperty());
        bgImageView.setPreserveRatio(false); // 不保持宽高比，填满容器
        bgImageView.setSmooth(true); // 开启平滑渲染
    }
    public void display() throws IOException {
        HashMap<String, String[]> userMap = UserLogin.deptAccountToNepc();
        String phoneNumber = txt_username.getText();
        String password = txt_password.getText();
        if (userMap.containsKey(phoneNumber)) {

            if (password.equals(userMap.get(phoneNumber)[0])) {

                currentLoggedInUserId = phoneNumber;
//                AlertUtils.showDialogs("用户登录", "登录成功");
                System.out.println("login success................");
                FXMLLoader fxmlLoader = new FXMLLoader();
                URL url = getClass().getResource("../view/AdminView.fxml");
                fxmlLoader.setLocation(url);
                Parent root = fxmlLoader.load();
                AdminViewController adminViewController = fxmlLoader.getController();
                Scene scene = new Scene(root);
                Stage stage =(Stage)(txt_username.getScene().getWindow());
                stage.setScene(scene);
                stage.show();

            } else {

                AlertUtils.showDialogs("用户登录", "密码不正确");
            }
        } else {

            AlertUtils.showDialogs("用户登录", "用户不存在");
        }
    }
}
